package com.czh.www.thread;

import com.czh.www.domain.DataPacket;
import com.czh.www.domain.PacketQueue;
import com.czh.www.domain.SystemVar;
import com.czh.www.domain.UsersVo;
import com.czh.www.util.GuiUtils;
import com.czh.www.util.NetUtils;

import java.util.logging.Level;
import java.util.logging.Logger;

public class DataPacketHandler implements Runnable {
    public DataPacketHandler() {
    }

    public void run() {
        while(true) {
            try {
                SystemVar.PACKET_QUEUE_FULL.acquire();
                DataPacket ex = PacketQueue.popPacket();
                SystemVar.PACKET_QUEUE_EMPTY.release();
                DataPacket dp;
                switch(ex.getCommandFunction()) {
                case 1:
                    SystemVar.addUsers(UsersVo.changeDataPacket(ex));
                    SystemVar.getUserListGui().updateUserNum(SystemVar.getUserList().size());
                    SystemVar.getUserListGui().updateUserList(SystemVar.getUserList());
                    dp = new DataPacket(3);
                    dp.setAdditional(SystemVar.USER_NAME + '\u0000' + "");
                    dp.setIp(NetUtils.getLocalHostIp());
                    UsersVo user = UsersVo.changeDataPacket(dp);
                    SystemVar.addUsers(user);
                    NetUtils.sendUdpPacket(dp, ex.getIp());
                    break;
                case 3:
                    if(SystemVar.addUsers(UsersVo.changeDataPacket(ex))) {
                        SystemVar.getUserListGui().updateUserNum(SystemVar.getUserList().size());
                        SystemVar.getUserListGui().addUserVo(UsersVo.changeDataPacket(ex));
                    }
                    break;
                case 32:
                    if((256 & ex.getOption()) != 0) {
                        dp = new DataPacket(33);
                        dp.setAdditional(Integer.toString(ex.getPacketNo()));
                        dp.setIp(ex.getIp());
                        NetUtils.sendUdpPacket(dp, dp.getIp());
                    }

                    GuiUtils.openMsgWindow(ex.getAdditional(), ex.getSenderName(), ex.getSenderHost(), ex.getIp());
                }
            } catch (InterruptedException var4) {
                Logger.getLogger(DataPacketHandler.class.getName()).log(Level.SEVERE, (String)null, var4);
            }
        }
    }
}
